using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Security.Permissions;

using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using Microsoft.ConnectionEngine.Common.Security;
using Microsoft.ConnectionEngine.Common;

namespace Microsoft.ConnectionEngine.Administration.Core.ServiceProvider.Business
{
   
    /// <summary>
    /// Exposes methods that allow communication with the Routing Services
    /// </summary>
    public class ServiceProviderTypeManager
    {
        /// <summary>
        /// Get a list of ServiceProviderTypes by search criteria
        /// </summary>
        /// <param name="searchString">If left empty (empty string) all  provider types are returned</param>
        /// <returns></returns>
        public static ServiceProviderTypes Search(HSDPrincipal currentPrincipal, ServiceProviderTypeSearchRequest searchRequest)
        {
            if (!SecurityManager.UserInRoles(currentPrincipal, new String[2]{RoleTypes.Administrator, RoleTypes.Provider}))
                throw new SecurityException();
            
            string searchRequestXml = AdapterHelper.ConstructObjectXml(searchRequest, typeof(ServiceProviderTypeSearchRequest));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, searchRequestXml);          

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1041, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return (ServiceProviderTypes)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(ServiceProviderTypes));             
        }

        /// <summary>
        /// Retrieves a ServiceProviderType for read only
        /// </summary>
        public static ServiceProviderType SelectItem(HSDPrincipal currentPrincipal, int serviceProviderTypeID)
        {            
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderTypeID", serviceProviderTypeID.ToString());

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1064, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return (ServiceProviderType)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(ServiceProviderType));
        }
        

        /// <summary>
        /// Locks a ServiceProviderType for the current user
        /// </summary>
        /// <param name="serviceProviderTypeID"></param>
        /// <returns>SeviceProviderType - If checkout successful, null otherwise </returns>
        public static ServiceProviderType Lock(HSDPrincipal currentPrincipal, int serviceProviderTypeID)
        {           
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderTypeID", serviceProviderTypeID.ToString());           

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1032, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return (ServiceProviderType)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(ServiceProviderType));
        }

        /// <summary>
        /// Unlocks a ServiceProviderType locked by the current user
        /// </summary>
        /// <param name="serviceProviderTypeID"></param>
        /// <returns>True - If checkout successful, false otherwise </returns>
        public static bool Unlock(HSDPrincipal currentPrincipal, int serviceProviderTypeID)
        {        
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderTypeID", serviceProviderTypeID.ToString());           
          
            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1031, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);            
        }

        /// <summary>
        /// Unlocks all ServiceProviderTypes locked by the current user
        /// </summary>
        /// <returns>True - If successful, false otherwise</returns>
        public static bool UnlockAll(HSDPrincipal currentPrincipal)
        {
            if (!SecurityManager.UserInRoles(currentPrincipal, new String[2]{RoleTypes.Administrator, RoleTypes.Provider}))
                throw new SecurityException();

            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderTypeUnlockAll", "");

            //string payload = @"<ServiceProviderTypeCheckInAll xmlns=""http://Microsoft.ConnectionEngine/Schemas/ServiceProviderRegister""></ServiceProviderTypeCheckInAll>";

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1039, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);            
        }


        /// <summary>
        /// Add a new service provider type to the Service Provider Register
        /// </summary>
        /// <param name="provider"></param>
        /// <returns>id of service provider type - int.minValue if unsuccessful</returns>
        /// <remarks>the field ServiceProviderID value can not be left empty when calling this method. It is recommended to ser the value of 
        /// ServiceProviderID to int.MinValue when creating a new provider. A valid ID will be assign to the provider by HSD</remarks>
        public static int Insert(HSDPrincipal currentPrincipal, ServiceProviderType serviceProviderType)
        {            
            string providerTypeXml = AdapterHelper.ConstructObjectXml(serviceProviderType, typeof(ServiceProviderType));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, providerTypeXml);
            
            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1034, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
           
            return ((ServiceProviderType)AdapterHelper.GetPayloadObjectFromResponse(result, typeof(ServiceProviderType))).ServiceProviderTypeID;     
        }

        /// <summary>
        /// Update an existing ServiceProviderType
        /// </summary>
        /// <param name="providertype"></param>
        /// <returns></returns>
        public static bool Update(HSDPrincipal currentPrincipal, ServiceProviderType serviceProviderType)
        {            
            string providerTypeXml = AdapterHelper.ConstructObjectXml(serviceProviderType, typeof(ServiceProviderType));
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, providerTypeXml);           

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1033, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);

            return AdapterHelper.GetBooleanFromResponse(result);                       
        }

        /// <summary>
        /// Deletes a ServiceProviderType
        /// </summary>
        /// <param name="serviceProviderTypeID"></param>
        /// <returns></returns>
        public static bool Delete(HSDPrincipal currentPrincipal, int serviceProviderTypeID)
        {            
            string payload = AdapterHelper.ConstructPayloadXml(currentPrincipal, "ServiceProviderTypeID", serviceProviderTypeID.ToString());           

            ConnectionEngineMessage result = AdapterHelper.SendConnectionEngineMessage(
                1030, ConfigurationManager.AppSettings["ServiceProviderRegister.ServiceProviderID"], payload);
            return AdapterHelper.GetBooleanFromResponse(result);          
        }
    }
}
